//
//  CheckVersionAlertView.m
//  DepressionAuxiliary
//
//  Created by ZhenYu Wang on 2021/5/13.
//

#import "CheckVersionAlertView.h"
#import "AppDelegate.h"

@interface CheckVersionAlertView(){
    NSDictionary *configDic;
}

@property (weak, nonatomic) IBOutlet UIButton *leftBtn;
@property (weak, nonatomic) IBOutlet UIButton *rightBtn;
@property (weak, nonatomic) IBOutlet UIView *showView;
@property (weak, nonatomic) IBOutlet UILabel *contentLabel;
@property (weak, nonatomic) IBOutlet UIButton *removeBtn;
@property (weak, nonatomic) IBOutlet UIImageView *huojiImageView;
@property (weak, nonatomic) IBOutlet UIImageView *whiteImageView;
@property (weak, nonatomic) IBOutlet UIImageView *greenImageView;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;

@end

@implementation CheckVersionAlertView

- (id)initItem:(NSDictionary *)dic {
    self = [[[NSBundle mainBundle] loadNibNamed:@"CheckVersionAlertView" owner:self options:nil] lastObject];
    if (self) {
        configDic = dic;
        [self setFrame:CGRectMake(0, 0, SCREEN_WIDTH_DEVICE, SCREEN_HEIGHT_DEVICE)];
        float width = 260 * SCREEN_SCALE_320;
        float huojiHeight = (184/324.0) * width;
        float scale = huojiHeight/184.0;
        [self.huojiImageView setFrame:CGRectMake(0, 0, width, huojiHeight)];
        [self.greenImageView setFrame:CGRectMake(0, 39, width, huojiHeight)];
        self.whiteImageView.layer.masksToBounds = YES;
        self.whiteImageView.layer.cornerRadius = 20.0f;
        self.greenImageView.layer.masksToBounds = YES;
        self.greenImageView.layer.cornerRadius = 20.0f;
        [self.titleLabel setFrame:CGRectMake(0, 143*scale, width, 22)];
        
        self.titleLabel.text = [NSString stringWithFormat:@"您有新版本 v%@", [dic objectForKey:@"newVersion"]];
        NSString *contentStr = [dic objectForKey:@"updateLog"];
        self.contentLabel.text = contentStr;
        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
        paragraphStyle.lineSpacing = 4;// 字体的行间距
        CGSize labelSize = [contentStr boundingRectWithSize:CGSizeMake(width - 120, MAX_INPUT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14.0], NSParagraphStyleAttributeName:paragraphStyle} context:nil].size;
        NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:contentStr];
        [text addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, text.length)];
        self.contentLabel.attributedText = text;
        [self.contentLabel setFrame:CGRectMake(60, CGRectGetMaxY(self.titleLabel.frame) + 20, width - 60, labelSize.height)];

        float showViewHeight = CGRectGetMaxY(self.contentLabel.frame) + 110;
        [self.whiteImageView setFrame:CGRectMake(0, self.titleLabel.frame.origin.y, width,  showViewHeight - self.titleLabel.frame.origin.y)];
        [self.showView setFrame:CGRectMake((SCREEN_WIDTH_DEVICE-width)/2.0, (SCREEN_HEIGHT_DEVICE-showViewHeight)/2.0, width, showViewHeight)];
        

        if ([[dic objectForKey:@"forceUpdate"] boolValue]) {
            self.leftBtn.hidden = YES;
            float widthButton = 118*SCREEN_SCALE;
            [self.rightBtn setFrame:CGRectMake((width-widthButton)/2.0, CGRectGetMaxY(self.contentLabel.frame) + 38, widthButton, 40)];
            self.rightBtn.layer.masksToBounds = YES;
            self.rightBtn.layer.cornerRadius = 20.0f;
            [self.rightBtn setBackgroundColor:ThemeColor];
        } else {
            float widthButton = (width - 80 - 20)/2.0;
            [self.leftBtn setFrame:CGRectMake(40, CGRectGetMaxY(self.contentLabel.frame) + 38, widthButton, 40)];
            [self.rightBtn setFrame:CGRectMake(40 + widthButton + 20, CGRectGetMaxY(self.contentLabel.frame) + 38, widthButton, 40)];
            self.leftBtn.layer.masksToBounds = YES;
            self.leftBtn.layer.cornerRadius = 20.0f;
            self.leftBtn.layer.borderWidth = 1.0f;
            self.leftBtn.layer.borderColor = rgba(153, 153, 153, 1).CGColor;
            self.rightBtn.layer.masksToBounds = YES;
            self.rightBtn.layer.cornerRadius = 20.0f;
            [self.rightBtn setBackgroundColor:ThemeColor];
        }
        
        
    }
    return self;
}

- (void)show {
    AppDelegate *del = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    [del.window addSubview:self];
    self.showView.layer.opacity = 0.5f;
    self.showView.layer.transform = CATransform3DMakeScale(1.3f, 1.3f, 1.0);
    [UIView animateWithDuration:0.2f delay:0.0 options:UIViewAnimationOptionCurveEaseInOut
                     animations:^{
                         self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.3f];
                         self.showView.layer.opacity = 1.0f;
                         self.showView.layer.transform = CATransform3DMakeScale(1, 1, 1);
                     }
                     completion:NULL
     ];
}

- (IBAction)leftPress {
    [self removePress];
}

- (IBAction)rightPress {
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[configDic objectForKey:@"apkURL"]]];
    [self removePress];
}

- (IBAction)removePress {
    [self removeFromSuperview];
}

- (void)backgroundClickDisable {
    self.removeBtn.userInteractionEnabled = NO;
}
@end
